package com.smzd.framework.oauth2.actions;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.smzd.framework.oauth2.business.OAuth2ClientAppService;
import com.smzd.utils.SessionHolder;

/**
 * Controller for retrieving the model for and displaying the confirmation page
 * for access to a protected resource.
 * 
 * @author YuZhongda
 */
@Controller
public class AccessConfirmationController {

    @Autowired
    private OAuth2ClientAppService clientDetailsService;

    @RequestMapping("/oauth/confirm_access")
    public String getAccessConfirmation(Map<String, Object> model) throws Exception {
        AuthorizationRequest clientAuth = (AuthorizationRequest) SessionHolder.getObject("authorizationRequest");
        ClientDetails client = clientDetailsService.loadClientByClientId(clientAuth.getClientId());
        model.put("clientApp", client);
        return "oauth2/access_confirmation.ftl";
    }

    @RequestMapping("/oauth/error")
    public String handleError(Map<String, Object> model) throws Exception {
        model.put("message", "There was a problem with the OAuth2 protocol");
        return "oauth2/oauth_error.ftl";
    }

}